<script type="text/javascript">
	validation_agenda = null;
	agendalist_createimageWindows = null;
	editAgenda=null;
	deleteAgenda=null;
	switchAgendaPublish=null;
	$(document).ready(function () {
		/* DECLARATION VARIABLE */
		/* General Variable */
		var theme = 'fresh';
		var agendalist_source;
		var agendalist_dataAdapter;

		/* Area and Field Variable */
		var agendalist_menu = $("#agendalist_menu");
		var agendalist_content = $("#agendalist_content");
		var agendalist_expander = $("#agendalist_expander");
		var agendalist_form = $("#agendalist_form");
		var agendalist_gridview = $("#agendalist_gridview");
		var agendalist_search_textfield = $("#agendalist_search_textfield");
		var agendalist_searchbutton = $("#agendalist_searchbutton");
		var agendalist_addbutton = $("#agendalist_addbutton");
		var agendalist_editbutton = $("#agendalist_editbutton");
		var agendalist_deletebutton = $("#agendalist_deletebutton");
		var agendalist_resetbutton = $("#agendalist_resetbutton");
		var agendalist_validasiwindow = $("#agendalist_validasiwindow");
		var agendalist_btnokValidasiWindow = $("#agendalist_btnokValidasiWindow");
		var agendalist_btnsaveValidasiWindow = $("#agendalist_btnsaveValidasiWindow");
		var agendalist_contentValidasiWindow = $("#agendalist_contentValidasiWindow");
		var agendalist_syaratgridview = $("#agendalist_syaratgridview");
		var agendalist_nomasukValidasiWindow = $("#agendalist_nomasukValidasiWindow");
		var agendalist_tglmasukValidasiWindow = $("#agendalist_tglmasukValidasiWindow");
		var agendalist_tanggal_awal = $("#agendalist_tanggal_awal");
		var agendalist_tanggal_akhir = $("#agendalist_tanggal_akhir");

		/* END DECLARATION VARIABEL */

		/* SET TYPE AND THEME */
		$('.field_input').addClass('jqx-input');
		$('.field_input').addClass('field');
		$('.field_input').addClass('jqx-rc-all');

		agendalist_tanggal_awal.jqxDateTimeInput({
			formatString : 'dd-MM-yyyy',
			width : 150,
			height : 25,
			theme : theme,
			enableAbsoluteSelection: true
		});
		agendalist_tanggal_akhir.jqxDateTimeInput({
			formatString : 'dd-MM-yyyy',
			width : 150,
			height : 25,
			theme : theme,
			enableAbsoluteSelection: true
		});

		agendalist_menu.jqxMenu({ align: 'left', width: '925px', height: '27px', autoOpen: false, autoCloseOnMouseLeave: false, showTopLevelArrows: true, theme: theme });
		agendalist_menu.css('visibility', 'visible');
		agendalist_addbutton.jqxButton({ theme : theme, width : '70px', height : '18px' });
		agendalist_editbutton.jqxButton({ width : '70px' , height : '18px' ,theme : theme });
		agendalist_deletebutton.jqxButton({ width : '70px' , height : '18px' ,theme : theme });
		agendalist_searchbutton.jqxButton({ theme : theme, width : '70px', height : '18px' });
		agendalist_resetbutton.jqxButton({ theme : theme, width : '70px', height : '18px' });
		agendalist_btnokValidasiWindow.jqxButton({ theme : theme, width : 70, height : 25 });
		agendalist_btnsaveValidasiWindow.jqxButton({ theme : theme, width : 120, height : 25 });
		agendalist_validasiwindow.jqxWindow({ width: 800, height: 350, resizable: false, theme: theme, isModal: true, autoOpen: false, okButton: agendalist_btnokValidasiWindow, modalOpacity: 0.4, showCloseButton: false });



		var agendalist_source = {
			id : 'id',
			datatype : "json",
			url : "<?php echo base_url(); ?>c_agenda/agenda_list",
			root : "results",
			datafields : [
				{ name : 'id' },
				{ name : 'agenda' },
				{ name : 'tempat' },
				{ name : 'tanggal' },
				{ name : 'jam' },
				{ name : 'keterangan' },
				{ name : 'publish' }
			],
			beforeprocessing : function(data){
				agendalist_source.totalrecords = data[0].TotalRows;
			}
		}
		var agendalist_dataAdapter = new $.jqx.dataAdapter(agendalist_source);

		agendalist_gridview.jqxGrid({
			source : agendalist_dataAdapter,
			width : 925,
			theme : theme,
			pageable : true,
			autoheight : true,
			sortable : true,
			filterable : true,
			columnsresize : true,
			virtualmode : true,
			pagesize : 15,
			pagesizeoptions : [15],
			rendergridrows : function(){
				return agendalist_dataAdapter.records;
			},
			columns : [
				{text : 'Pb', dataField : 'publish', width : 30,cellsrenderer : function(row){
						var record = agendalist_gridview.jqxGrid('getrowdata', row);
						if(record.publish == 'y'){
							return '<img src="<?php echo base_url(); ?>images/buttons/ok.png" onClick="doSwitchAgendaPublish(event, \'' + record.id + '\', \'t\')" '+
						'title="Unpublish" style="margin:2px 7px 2px 7px;"/>';
						}else{
							return '<img src="<?php echo base_url(); ?>images/icons/cross.gif" onClick="doSwitchAgendaPublish(event, \'' + record.id + '\', \'y\')" '+
						'title="Publish" style="margin:2px 7px 2px 7px;"/>';
						}
					}
				},
				{text : 'Agenda', dataField : 'agenda', width : 400},
				{text : 'Tempat', dataField : 'tempat', width: 300},
				{text : 'Tanggal', dataField : 'tanggal', width: 100},
				{text : 'Jam', dataField : 'jam', width: 80},
				{text : 'Action', width : 70,cellsrenderer : function(row){
						var record = agendalist_gridview.jqxGrid('getrowdata', row);
						var agenda_id = record.id;
						return '<img src="<?php echo base_url(); ?>images/icons/edit.png" id="agendaedit' + row + '" onClick="doEditAgenda(event, \'' + agenda_id + '\')" '+
						'title="Edit Data" style="margin:2px 7px 2px 7px;"/>' + 
						'<img src="<?php echo base_url(); ?>images/icons/delete.png" id="agendadelete' + row + '" onClick="doDeleteAgenda(event, \'' + agenda_id + '\')" '+
						'title="Delete Data" style="margin:2px 7px 2px 7px;"/>';
					}
				}
			]
		});


		/* DECLARATION FUNCTION */
		function reset_form(){
		}
		function refresh_grid(){
			agendalist_gridview.jqxGrid('updatebounddata');
		}

		agendalist_addbutton.click(function(){
			$('#agendalist_content').empty();
			$('<img src="./images/loader.gif" style="margin:50px auto auto 390px;">').appendTo('#contents');
			$('#agendalist_content').load('index.php/c_agenda/agenda_addedit');
		});

		agendalist_editbutton.click(function(){
			var agenda_id = '';
			var rowindex = agendalist_gridview.jqxGrid('getselectedrowindex');
			var data_record = agendalist_gridview.jqxGrid('getrowdata', rowindex);
			if(data_record !== null){
				agenda_id = data_record.id;
				$('#agendalist_content').empty();
				$('<img src="./images/loader.gif" style="margin:50px auto auto 390px;">').appendTo('#contents');
				$('#agendalist_content').load('index.php/c_agenda/agenda_addedit/'+agenda_id);
			}else{
				alert("Pilih salah satu field yang ingin diubah");
			}
		});

		agendalist_searchbutton.click(function(){
			var tanggal_awal = agendalist_tanggal_awal.jqxDateTimeInput('getDate');
			tanggal_awal = $.jqx.dataFormat.formatdate(tanggal_awal, 'yyyy-MM-dd');
			var tanggal_akhir = agendalist_tanggal_akhir.jqxDateTimeInput('getDate');
			tanggal_akhir = $.jqx.dataFormat.formatdate(tanggal_akhir, 'yyyy-MM-dd');
			var search_text = agendalist_search_textfield.val();
			agendalist_source['url'] = "<?php echo base_url(); ?>c_agenda/agenda_list?query=" + search_text + "&tglawal=" + tanggal_awal + "&tglakhir=" + tanggal_akhir;
			agendalist_gridview.jqxGrid('gotopage',0);
			agendalist_dataAdapter.dataBind();
		});
		agendalist_resetbutton.click(function(){
			agendalist_search_textfield.val("");
			agendalist_source['url'] = "index.php/c_agenda/agenda_list/";
			agendalist_gridview.jqxGrid('gotopage',0);
			agendalist_dataAdapter.dataBind();
		});

		agendalist_deletebutton.click(function(){
			var rowindex = agendalist_gridview.jqxGrid('getselectedrowindex');
			var data_record = agendalist_gridview.jqxGrid('getrowdata', rowindex);
			if(data_record !== null){

			if(window.confirm("Apakah anda yakin menghapus data ini ? ")){
				$(document).ready(function () {
					$.ajax({
						url : 'index.php/c_agenda/agenda_delete',
						type : 'POST',
						data : {
							id : id
						}, success : function(msg){
							var result = eval(msg);
							if(result == 1){
								$("#agendalist_gridview").jqxGrid('updatebounddata');
								alert("Data sukses dihapus");
							}else{
								alert("Data gagal dihapus");
							}
						}
					});

				});
			}
		}

		});

		function agendalist_view(event, type ,file){
			agendalist_createimageWindows(type ,file);
		}

		function agendalist_edit(event, id) {
			self.location='index.php/c_agenda/get_action&task=update/' + id;
		}

		function agendalist_validasi(event, row) {
			validation_permohonan(row);
		}
		
		editAgenda = function(agenda_id){
			$('#agendalist_content').empty();
			$('<img src="<?php echo base_url(); ?>images/loader.gif" style="margin:50px auto auto 390px;">').appendTo('#contents');
			$('#agendalist_content').load('<?php echo base_url(); ?>c_agenda/agenda_addedit/'+agenda_id);
		}
		deleteAgenda = function(agenda_id){
			if(window.confirm("Apakah anda yakin menghapus data ini ? ")){
				$(document).ready(function () {
					$.ajax({
						url : '<?php echo base_url(); ?>c_agenda/agenda_delete',
						type : 'POST',
						data : {
							id : agenda_id
						}, success : function(msg){
							var result = eval(msg);
							if(result == 1){
								$("#agendalist_gridview").jqxGrid('updatebounddata');
								alert("Data sukses dihapus");
							}else{
								alert("Data gagal dihapus");
							}
						}
					});

				});
			}
		}
		switchAgendaPublish = function(agenda_id, stats){
			$.ajax({
				url : '<?php echo base_url(); ?>c_agenda/agenda_switchpublish',
				type : 'POST',
				data : {
					id : agenda_id,
					stats : stats
				}, success : function(msg){
					var result = eval(msg);
					if(result == 1){
						$("#agendalist_gridview").jqxGrid('updatebounddata');
						alert("Data sukses dirubah");
					}else{
						alert("Data gagal dirubah");
					}
				}
			});
		}

});
function doEditAgenda(event, agenda_id){
	editAgenda(agenda_id);
}
function doDeleteAgenda(event, agenda_id){
	deleteAgenda(agenda_id);
}
function doSwitchAgendaPublish(event, agenda_id, stats){
	switchAgendaPublish(agenda_id, stats);
}
</script>
<style type="text/css">
	.field{
		height : 22px;
	}
</style>
	<!-- Begin one column window -->
<br class="clear"/>
<div class="header">
	<span><h1>AGENDA</h1></span>
</div>
<div id="agendalist_content">
	<div id="agendalist_menu">
		<div id='agendalist_addbutton' style="float:left;">
			<img style='float: left; margin: 0px;' src='./images/icons/add.png' />
			<div style='float: left; margin-left: 4px;'>Baru</div>
		</div>
		<!-- <div id='agendalist_editbutton' style="float:left;">
			<img style='float: left; margin: 0px;' src='./images/icons/page_edit.png' />
			<div style='float: left; margin-left: 4px;'>Ubah</div>
		</div>
		<div id='agendalist_deletebutton' style="float:left;">
			<img style='float: left; margin: 0px;' src='./images/icons/delete.png' />
			<div style='float: left; margin-left: 4px;'>Hapus</div>
		</div> -->
		<div style="float:left;">&nbsp;Tanggal : </div>
		<div id="agendalist_tanggal_awal" style="float:left;"></div>
		<div style="float:left;">&nbsp;s/d&nbsp;</div>
		<div id="agendalist_tanggal_akhir" style="float:left;"></div>
		<div style="float:left;">
			<input type="text" id="agendalist_search_textfield" class="field_input" style="height: 24px; width : 200px; background-color : #FFFFFF;">
		</div>
		<div id='agendalist_searchbutton' style="float:left;">
			<img style='float: left; margin: 0px;' src='./images/icons/search.gif' />
			<div style='float: left; margin-left: 4px;'>Cari</div>
		</div>
		<div id='agendalist_resetbutton' style="float:left;">
			<img style='float: left; margin: 0px;' src='./images/icons/table_refresh.png' />
			<div style='float: left; margin-left: 4px;'>Reset</div>
		</div>
	</div>
	<div id="agendalist_gridview"></div>
</div>